package com.duoec.doc.service.impl.mock;

import com.duoec.doc.entity.MockConfigItem;
import com.duoec.doc.helper.MockHelper;
import com.duoec.doc.service.IMocker;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;

import java.util.Random;

/**
 * @author xuwenzhen
 * @date 2020/2/1
 */
@Service
public class CollectionTypeMocker implements IMocker {
    private static final Logger logger = LoggerFactory.getLogger(CollectionTypeMocker.class);

    /**
     * mock配置输出的响应
     *
     * @param item 配置
     * @return
     */
    @Override
    public String mock(MockConfigItem item) {
        if (!MockHelper.available(item)) {
            return null;
        }
        StringBuilder sb = new StringBuilder();
        sb.append("[");
        if (!CollectionUtils.isEmpty(item.getChildren())) {
            Integer mockType = item.getMockType();
            Boolean mockListLoopStructCreated = item.getMockListLoopStructCreated();
            if (mockType == 0 && mockListLoopStructCreated != null && mockListLoopStructCreated) {
                //差异性开启
                int count = item.getChildren().size();
                for (int i = 0; i < count; i++) {
                    MockConfigItem child = item.getChildren().get(i);
                    IMocker mocker = MockHelper.getMocker(child);
                    sb.append(mocker.mock(child));
                    if (i != count - 1) {
                        //不是最后一个
                        sb.append(",");
                    }
                }
                item.getChildren().forEach(child -> {
                    IMocker mocker = MockHelper.getMocker(child);
                    if (mocker != null) {

                    }
                });
            } else {
                MockConfigItem child = item.getChildren().get(0);
                IMocker mocker = MockHelper.getMocker(child);
                if (mocker != null) {
                    int count;
                    switch (mockType) {
                        case 0:
                            //固定循环次数
                            count = item.getMockValueStart();
                            break;
                        case 1:
                            //随机循环次数
                            Integer start = item.getMockValueStart();
                            Integer end = item.getMockValueEnd();
                            if (start == null) {
                                start = 0;
                            }
                            if (end == null) {
                                end = 100;
                            }
                            count = start + new Random().nextInt(end - start);
                            break;
                        default:
                            count = 0;
                            break;
                    }


                    for (int i = 0; i < count; i++) {
                        sb.append(mocker.mock(child));
                        if (i != count - 1) {
                            //不是最后一个
                            sb.append(",");
                        }
                    }
                }
            }
        }

        sb.append("]");
        return sb.toString();
    }

    /**
     * 可处理的基础类型: 0=基本类型 1=枚举 2=pojo 3=collection 4=map 5=数组
     *
     * @return 类型
     */
    @Override
    public Integer getMockType() {
        return 3;
    }
}
